package main

import (
	"flag"
	"fmt"
	"lancer/client"
	"lancer/common"
	"lancer/conf"
	"lancer/worker"
	"log"
)

//上海交行测试服编译上传脚本

var (
	confFile string
)

func initArgs() {
	flag.StringVar(&confFile, "config", "./conf/config-shjh.json", "指定config.json文件")
	flag.Parse()
}

func main() {
	var (
		err    error
		n      int
		output string
	)

	log.Println("ai_call_system web dev start...")

	//初始化命令行
	initArgs()

	//读取配置文件
	if err = conf.InitConfig(confFile); err != nil {
		log.Println("加载配置文件错误:", err)
		panic(err)
	}

	//初始化执行器
	if err = worker.InitExecutor(); err != nil {
		log.Println("执行器错误:", err)
		panic(err)
	}

	inputStr := "system"
	//等待用户确认再往下执行
	common.Ask(inputStr)

	//本地打包
	log.Println("本地打包")
	output = worker.GExecutor.ExecuteJob("cd /Users/wensiyao/code/go/world/ai/auth/ai_call/ && make linux")

	log.Println(output)

	//连接ssh
	if err = client.InitSsh(); err != nil {
		log.Println("ssh服务器错误:", err)
		panic(err)
	}

	//删除原来的包
	log.Println("删除服务器包")
	if output, err = client.GSshClientHandle.Run("rm -rf /home/ai-install/ai-docker-install/ai-call-sys/service/ai_call_system"); err != nil {
		log.Println("ssh服务器删除文件错误:", err)
		panic(err)
	}
	log.Println(output)

	//上传包
	log.Println("上传本地包到服务器")
	if n, err = client.GSshClientHandle.UploadFile("/Users/wensiyao/code/go/world/ai/auth/ai_call/bin/ai-call-system-linux64", "/home/ai-install/ai-docker-install/ai-call-sys/service/ai_call_system"); err != nil {
		panic(err)
	}
	fmt.Println(n)

	//chmod 755 包 权限
	log.Println("给包赋值755权限")
	//生产服 /home/xy_info/aicall
	if output, err = client.GSshClientHandle.Run("chmod 755 /home/ai-install/ai-docker-install/ai-call-sys/service/ai_call_system"); err != nil {
		log.Println("ssh服务器chmod 755错误:", err)
		panic(err)
	}

	//执行docker restart ai_call_flow
	log.Println("docker ai_call_system 重启...")
	if output, err = client.GSshClientHandle.Run("docker restart ai_call_system"); err != nil {
		log.Println("ssh服务器ps -ef错误:", err)
		panic(err)
	}

	log.Println("ai_call_system 执行完成")

}
